package com.itstudio.portal.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itstudio.portal.entity.Institutions;
import com.itstudio.portal.entity.Project;
import com.itstudio.portal.entity.RestResponse;
import com.itstudio.portal.entity.dto.InstitutionsDto;
import com.itstudio.portal.entity.vo.InstitutionsVo;
import com.itstudio.portal.mapper.InstitutionsMapper;
import com.itstudio.portal.service.InstitutionsService;
import com.itstudio.portal.utils.BeanCopyUtil;
import com.itstudio.portal.utils.Const;
import com.itstudio.portal.utils.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * (Institutions)表服务实现类
 *
 * @author makejava
 * @since 2024-11-18 16:06:20
 */
@Service("institutionsService")
public class InstitutionsServiceImpl extends ServiceImpl<InstitutionsMapper, Institutions> implements InstitutionsService {

    @Autowired
    JwtUtil jwtUtil;
    //前台展示合作机构
    @Override
    public RestResponse getList() {
        List<Institutions> list = list();
        List<InstitutionsVo> institutionsVos = BeanCopyUtil.classListVo(list, InstitutionsVo.class);
//        InstitutionsVo institutionsVo = BeanCopyUtil.classVo(list, InstitutionsVo.class);
        return RestResponse.success(institutionsVos);
    }
    //后台删除合作机构
    @Override
    public RestResponse deleteinstitutions(Long id) {
        removeById(id);
        return RestResponse.success();
    }
    //后台修改合作机构
    @Override
    public RestResponse updateinstitutions(InstitutionsDto institutionsDto, HttpServletRequest request) {
        Institutions institutions = BeanCopyUtil.classVo(institutionsDto, Institutions.class);
        String token = request.getHeader("Authorization").substring(7);
        String id = jwtUtil.verifyToken(token);
        //设置修改人
        institutions.setCreateBy(Long.valueOf(id));
        //更新数据
        updateById(institutions);
        return RestResponse.success();
    }
//后台添加合作机构数据
    @Override
    public RestResponse addinstitutions(InstitutionsDto institutionsDto, HttpServletRequest request) {
        Institutions institutions = BeanCopyUtil.classVo(institutionsDto, Institutions.class);
        String token = request.getHeader("Authorization").substring(7);
        String id = jwtUtil.verifyToken(token);
        institutions.setCreateBy(Long.valueOf(id));
        save(institutions);
        return RestResponse.success();
    }

    @Override
    public RestResponse getListById(Long id) {
        Institutions institutions = getById(id);
        InstitutionsVo institutionsVo = BeanCopyUtil.classVo(institutions, InstitutionsVo.class);
        return RestResponse.success(institutionsVo);
    }


}
